using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }
    public int sunSum;//阳光总数
    public GameObject sunPrefab;
    public int curLevelId = 1; //当前关卡
    public int curProgressId = 1;//当前进度
    public List<Dictionary<string, string>> listData;//当前关卡数据
    public bool isStart;//是否开始游戏
    public bool isRemove;//是否移除模式
    public Button btnRemove;
    public Button btnPause;
    public Texture2D target;

    private void Awake()
    {
        curLevelId = int.Parse(LevelItemData.Instance.item.levelId);

        Instance = this;
        //初始化配置表
        GameConfigManager.Instance.Init();
        //获取当前关卡数据
        listData = GameConfigManager.Instance.GetLevelList(curLevelId.ToString());
        if (listData == null) Debug.Log("没有配置关卡数据");
    }

    private void Start()
    {
        AudioManager.Instance.PlayMusic("bgm3");
        btnRemove.onClick.AddListener(OnRemove);
        btnPause.onClick.AddListener(() =>{
            AudioManager.Instance.PlaySFX("暂停");
            UI.UIManager.Instance.OpenPanel(UI.UIConst.MenuPanel);
        });
    }

    void OnRemove()
    {
        if(!isStart) return;
        isRemove = !isRemove;
        if(isRemove){
            Cursor.SetCursor(target, new Vector2(0, target.height), CursorMode.Auto);
        }else{
            Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
        }
    }

    //设置阳光数
    public void SetSunSum(int num)
    {
        sunSum += num;
        UIManager.Instance.Init();
    }

    //顶部随机位置掉落阳光
    public void CreateSunDown()
    {
        //获取左下角、右上角的世界坐标
        Vector3 leftBottom = Camera.main.ViewportToWorldPoint(Vector2.zero);
        Vector3 rightTop = Camera.main.ViewportToWorldPoint(Vector2.one);
        float x = Random.Range(leftBottom.x + 50, rightTop.x - 50);
        Vector3 bornPos = new Vector3(x, rightTop.y, 0);
        GameObject sun = Instantiate(sunPrefab, bornPos, Quaternion.identity);
        float y = Random.Range(leftBottom.y + 50, rightTop.y - 120);
        sun.GetComponent<Sun>().SetTargetPos(new Vector3(x, y, 0));
    }

    //开始游戏
    public void StartGame()
    {
        AudioManager.Instance.PlaySFX("点击");
        AudioManager.Instance.PlayMusic("bgm1");
        isStart = true;
        //顶部随机位置掉落阳光
        InvokeRepeating("CreateSunDown", 10, 10);

        //生成僵尸
        GenerateZombies.Instance.TableCreateZombie();
    }
}
